package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.CustomActionData
import kotlinx.android.synthetic.main.custom_dialog_add_action_item.view.*

/**
 * CustomDialogActionAdapter
 *
 * @author  Liu.Heng
 * @date  2019/6/1
 */
class CustomDialogActionAdapter(val context: Context, private var mList: List<CustomActionData>, var call: OnItemClickListener) : RecyclerView.Adapter<CustomDialogActionAdapter.MyViewHolder>() {

    /**设定一个选中的标志位，在activity中传入值*/
    private var checked = -1

    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    private val mCallback = call

    interface OnItemClickListener {

        fun onItemClick(view: View)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
        val view = layoutInflater.inflate(R.layout.custom_dialog_add_action_item, p0, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(myViewHolder: MyViewHolder, position: Int) {
        val actionImage = myViewHolder.actionImage
        val nameText = myViewHolder.nameText
        val selectImage = myViewHolder.selectImage
        if (checked == position) {
            selectImage.visibility = View.VISIBLE
        } else {
            selectImage.visibility = View.GONE
        }
        nameText.text = mList[position].actionName
        actionImage.tag = position
        actionImage.setOnClickListener {
            mCallback.onItemClick(it)
        }
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    fun setChecked(checked: Int) {
        this.checked = checked
    }

    fun notify(mList: List<CustomActionData>) {
        this.mList = mList
        notifyDataSetChanged()
    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var actionImage: RelativeLayout = view.action_image
        var nameText: TextView = view.action_name
        var selectImage: ImageView = view.action_sel
    }
}